package com.hainatong.shiphelper.module.warehouse.viewmodel;

import java.util.List;

import com.hainatong.shiphelper.api.ApiResult;
import com.hainatong.shiphelper.api.ApiService;
import com.hainatong.shiphelper.api.VMObserver;

import com.hainatong.shiphelper.module.warehouse.entity.WareShelfBean;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.LiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import io.reactivex.disposables.Disposable;

public class ShelfViewModel extends BaseViewModel
{

    public ShelfViewModel(@NonNull Application application)
    {
        super(application);
    }


    private MutableLiveData<LiveResult<List<WareShelfBean>>> shelfResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<List<WareShelfBean>>> getShelfResult()
    {
        return shelfResult;
    }

    /**
     *  查询 货架
     * @param name
     */
    public void queryShelfByWareName(String name)
    {
        ApiService.Builder.getChanBoServer()
                          .queryShelfByWareHouseName( name )
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<List<WareShelfBean>>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                  addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getShelfResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<List<WareShelfBean>> result)
                              {
                                  //{"data":[{"children":[{"uuid":"20e3c954-e86c-4e75-b7ff-f054f561b92f","code":"NO6-002"}],"shelfName":"1号货架","floors":"2"}],"status":true,"code":"200","message":"操作成功"}
                                  if( result.isOk() )
                                  {
                                      getShelfResult().postValue( new LiveResult<>( true , result.getData() ) );
                                  }
                                  else
                                  {
                                      getShelfResult().postValue( new LiveResult<>( false , result.getMessage() ) );
                                  }
                              }
                          });

    }


}
